//
//  VisitorView.swift
//  bo
//
//  Created by 肖全刚 on 16/4/27.
//  Copyright © 2016年 肖全刚. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)
        setUI()
    }
    
    //MARK: - 外界方法
    func setupVisitorInfo(imageName: String?, title: String)
    {
        self.title.text = title
        
        guard let name = imageName else{
            
            //动画
            startAnimation()
            return
        }
        
        //不是首页
        self.icon.image = UIImage(imageLiteral: name)
        
        homeIcon.hidden =  true
    }

    //添加动画
    func startAnimation()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        anim.removedOnCompletion = false
        
        homeIcon.layer.addAnimation(anim, forKey: nil)
    }

    
    // MARK: - 懒加载
    
    //转盘
    private lazy var homeIcon:UIImageView =  UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //图标
    lazy var icon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文本
    lazy  var title: UILabel = {
        var lb = UILabel()
        lb.numberOfLines = 0
        lb.textColor = UIColor.lightGrayColor()
        lb.text = "🐧🐧拿冠军"
        lb.textAlignment = NSTextAlignment.Center
        return lb
    }()
    
    
    //注册
    lazy var registerButton:UIButton =
    {
        var registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(imageLiteral:"common_button_white_disable"), forState: UIControlState.Normal)
        
        return registerBtn
    }()
    //登录
    lazy var loginButton:UIButton = {
       
        var loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(imageLiteral:"common_button_white_disable"), forState: UIControlState.Normal)

        return loginBtn
        
    }()
    private lazy var maskImageView:UIImageView = UIImageView(image: UIImage(imageLiteral:"visitordiscover_feed_mask_smallicon"))
    
}


extension VisitorView
{
    //MARK: - 初始化UI
    private func setUI()
    {
        addSubview(maskImageView)
        addSubview(homeIcon)
        addSubview(icon)
        addSubview(title)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //转盘
        homeIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //图标
        icon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(15)
        }
        
        //文字
        title.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.top.equalTo(homeIcon.snp_bottom).offset(15)
            make.width.equalTo(255)
        }
        
        //注册
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(title.snp_bottom).offset(15)
            make.left.equalTo(title.snp_left)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registerButton)
            make.right.equalTo(title.snp_right)
            make.width.equalTo(100)
        }
        
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            
        }
    }
}
